package com.hjj.dataStructures.stack;

public class Calculator {
    public static void main(String[] args) {
        String expression = "3+2*6-2";
        // 创建两个栈，数栈，一个符号栈
        ArrayStack1 numStack = new ArrayStack1(10);
        ArrayStack1 operStack = new ArrayStack1(10);
        // 定义需要的相关变量
        int index = 0, num1 = 0, num2 = 0, oper = 0, res = 0;
        char ch = ' ';
        while (true) {
            ch = expression.substring(index, index + 1).charAt(0);
            if (operStack.isOper(ch)) {
                if (operStack.isEmpty()) {
                    // 直接入栈
                    operStack.push(ch);
                } else {
                    // 如果符号栈有操作符，就进行比较，如果当前的操作符的优先级小于或者等于栈中的操作符，就需要从数栈中pop出两个数
                    // 在从符号栈中pop出一个符号，进行运算，将得到结果，入数栈，然后将当前的操作符入符号栈
                    if (operStack.priority(ch) <= operStack.priority(operStack.peek())) {
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        oper = operStack.pop();
                        res = numStack.cal(num1, num2, oper);
                        // 把运算的结果如数栈
                        numStack.push(res);
                        // 将当前的操作符入符号栈
                        operStack.push(ch);
                    } else {
                        operStack.push(ch);
                    }
                }
            } else {
                numStack.push(ch - 48);
            }
            index++;
            if (index >= expression.length()) break;
        }
        while (true) {
            if (operStack.isEmpty()) break;
            num1 = numStack.pop();
            num2 = numStack.pop();
            oper = operStack.pop();
            res = numStack.cal(num1, num2, oper);
            numStack.push(res);
        }
        System.out.println("表达式" + expression + "=" + numStack.pop());
    }

}


/**
 * 定义一个用Array模拟的栈
 */
class ArrayStack1 {
    private int maxSize;
    private int[] stack;
    private int top = -1; // top表示栈顶，初始化为-1

    public ArrayStack1(int maxSize) {
        this.maxSize = maxSize;
        this.stack = new int[this.maxSize];
    }

    public boolean isFull() {
        if (top == (maxSize - 1)) {
            System.out.println("栈满");
            return true;
        } else {
            return false;
        }
    }

    public boolean isEmpty() {
        if (top == -1) {
            System.out.println("栈空");
            return true;
        } else {
            return false;
        }

    }

    /**
     * 入栈
     *
     * @param value
     */
    public void push(int value) {
        if (isFull()) return;
        top++;
        stack[top] = value;
    }

    /**
     * 出栈
     *
     * @return
     */
    public int pop() {
        if (isEmpty()) return -1;
        int value = stack[top];
        top--;
        return value;
    }

    /**
     * 不是pop
     *
     * @return 返回当前栈顶的值
     */
    public int peek() {
        return stack[top];
    }

    /**
     * 显示栈的情况【遍历栈】
     */
    public void list() {
        if (isEmpty()) return;
        // 需要从栈顶开始显示数据
        for (int i = top; i >= 0; i--) {
            System.out.printf("stack[%d]=%d\n", i, stack[i]);
        }
    }

    /**
     * <p>返回运算符的优先级，优先级是程序员来确定，优先级使用数字表示<p/>
     * <p>数字越大，则优先级就越高</p>
     *
     * @param oper 运算符
     * @return
     */
    public int priority(int oper) {
        if ('*' == oper || '/' == oper) {
            return 1;
        } else if ('+' == oper || '-' == oper) {
            return 0;
        } else {
            return -1; // 假定目前的表达式只有+，-，*，/
        }
    }

    /**
     * <p>判断是不是一个运算符</p>
     *
     * @param oper 运算符
     * @return
     */
    public boolean isOper(char oper) {
        return '+' == oper || '-' == oper || '*' == oper || '-' == oper;
    }

    /**
     * <p>计算方法</p>
     *
     * @return 返回计算结果
     */
    public int cal(int num1, int num2, int oper) {
        int res = 0;
        switch (oper) {
            case '+':
                res = num1 + num2;
                break;
            case '-':
                res = num2 - num1;
                break;
            case '/':
                res = num2 / num1;
                break;
            case '*':
                res = num2 * num1;
                break;
            default:
                break;
        }
        return res;
    }
}